/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.oszero.deliver.server.client.jpush;

import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.oszero.deliver.server.exception.MessageException;
import com.oszero.deliver.server.model.app.JPushApp;
import com.oszero.deliver.server.model.dto.common.SendTaskDto;
import com.oszero.deliver.server.util.MessageLinkTraceUtils;
import org.springframework.stereotype.Component;
import com.oszero.deliver.server.client.jpush.dtos.JPushParam;

import java.util.HashMap;

@Component
public class JPushClient
{
    /**
     * 发送消息
     * @param jPushApp 配置文件
     * @param sendTaskDto 发送内容
     */
    public void SendMessage(JPushApp jPushApp, SendTaskDto sendTaskDto) {
        try {
            JPushParam jPush = JPushParam.builder()
                    .title(sendTaskDto.getParamMap().get("title").toString())
                    .message(sendTaskDto.getParamMap().get("content").toString())
                    .alias(sendTaskDto.getParamMap().get("alias").toString())
                    .build();

            if (jPush.getExtraParam() == null)
                jPush.setExtraParam(new HashMap<>());
            //创建JPushClient
            cn.jpush.api.JPushClient jpushClient = new cn.jpush.api.JPushClient(jPushApp.getMasterSecret(),jPushApp.getAppKey());
            //创建option
            PushPayload payload = PushPayload.newBuilder()
                    .setPlatform(Platform.all())  //所有平台的用户
                    .setAudience(Audience.alias(jPush.getAlias()))
                    .setNotification(Notification.newBuilder()
                            .addPlatformNotification(IosNotification.newBuilder() //发送ios
                                    .setAlert(jPush.getMessage()) //消息体
                                    .setBadge(+1)
                                    .setSound("happy") //ios提示音
                                    .addExtras(jPush.getExtraParam()) //附加参数
                                    .build())
                            .addPlatformNotification(AndroidNotification.newBuilder() //发送android
                                    .addExtras(jPush.getExtraParam()) //附加参数
                                    .setAlert(jPush.getMessage()) //消息体
                                    .setTitle(jPush.getTitle())
                                    .build())
                            .build())
                    .setOptions(Options.newBuilder().setApnsProduction(jPushApp.getEnv()).build())//指定开发环境 true为生产模式 false 为测试模式 (android不区分模式,ios区分模式)
                    .setMessage(Message.newBuilder().setMsgContent(jPush.getMessage()).addExtras(jPush.getExtraParam()).build())//自定义信息
                    .build();

            PushResult pushResult = jpushClient.sendPush(payload);
            MessageLinkTraceUtils.recordMessageLifecycleInfoLog(sendTaskDto, "[极光推送]PushResult result is " + pushResult);
        } catch (Exception e) {
            throw new MessageException(sendTaskDto, "[极光推送]PushResult result is \" + pushResult" + e.getMessage());
        }
    }
}
